<?php

namespace App\Http\Controllers\V1\Admin;


use App\Models\V1\Notice;
use App\Models\V1\Users;
use App\Services\JPushService;
use Illuminate\Http\Request;

class NoticeController extends BaseController
{

    /**
     * 列表数据
     * @param Request $request
     * @return void
     */
    public function index(Request $request)
    {
        $pageSize = $request->input('pageSize', 10);
        $order = $request->input('order', 'desc');
        $sort = $request->input('sort', 'id');


        $query = Notice::with('NoticeClass');
        if ($request->filled('title')) {
            $query->where('title', 'like', "%" . $request->input('title') . "%");
        }

        if($request->filled('notice_class_id')){
            $query->where('notice_class_id',$request->input('notice_class_id',""));
        }

        $query->orderBy($sort, $order);
        $data = $query->paginate($pageSize);
        return ajax_output(200, "成功", $data);
    }

    /**
     * 添加公告
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request)
    {
        $user = auth('box_admin')->user();
        $notice_model = new Notice();

        $title = $request->input('title');
        $content = $request->input('content');
        $res = $notice_model->create([
            'title' => $title,
            'content' => $content,
            'state' => $request->input('state'),
            'remark' => $request->input('remark'),
            'notice_class_id'=>$request->input('notice_class_id',""),
            'preparatory_id'=>$user['id'],
            'preparatory_name'=>$user['username'],
            'community_id'=>$user['community_id'],
            'community_name'=>$user['community_name'],
        ]);
        $id =  $res->id;

        $JPushService = new JPushService();
        $message = [
            'title' => $title,
            'content' => $content,
        ];
        $extraParams ='zhimeng://notice?id='.$id;
        $registration_ids = Users::where('preparatory_id',$user['id'])->pluck('registration_id')->toArray();

        $JPushService->pushNotify($registration_ids,$message,$extraParams);
        return ajax_output(200, "成功",$registration_ids);
    }

    /**
     * 修改配置
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request, $id)
    {

        $user = auth('box_admin')->user();

        $notice_model = new Notice();
        $vo = $notice_model->find($id);
        $vo->update([
            'title' => $request->input('title'),
            'content' => $request->input('content'),
            'state' => $request->input('state'),
            'remark' => $request->input('remark'),
            'notice_class_id'=>$request->input('notice_class_id',""),
            'preparatory_id'=>$user['id'],
            'preparatory_name'=>$user['username'],
            'community_id'=>$user['community_id'],
            'community_name'=>$user['community_name'],
        ]);
        return ajax_output(200, "成功");
    }

    /**
     * 配置详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(Request $request, $id)
    {
        $vo = Notice::find($id);
        return ajax_output(200, "成功", $vo);
    }


    /**
     * 删除
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $id = explode(',',$id);
        Notice::destroy($id);
        return ajax_output(200, "成功");
    }
}
